package cn.qiao.threadDemo.domain;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * desc: 通过实现Callable接口中的run方法来执行一个多线程任务, 并可以获取一个返回值
 *
 * @author qiao
 * @date 2023年08月07日 22:45
 */
public class InterfaceCallable implements Callable {

    public Object call() throws Exception {
        long id = Thread.currentThread().getId();
        System.out.println("通过实现Runnable接口来开启一个多线程任务");
        Thread.sleep(1000);
        return id;

    }


    public static void main(String[] args) {
        FutureTask futureTask = new FutureTask(new InterfaceCallable());
        Thread thread = new Thread(futureTask);
        thread.start();
        Object o = null;
        try {
            o = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(o);
    }

}
